
import  requests

from common.get_headers import get_headers
from common.log_util import init_logger
from config import BASE_URL
from demo import body_modify, headers

logger=init_logger()
class EmployeeApi(object):
    @classmethod
    def add_emp(cls,headers,body):
        url=BASE_URL+"/api/sys/user"
        logger.debug(f"请求url:{url}")
        logger.debug(f"请求头:{headers}")
        logger.debug(f"请求体:{body}")
        resp = requests.post(url, headers=headers, json=body)
        logger.debug(f"响应体:{resp.json()}")          #打印日志
        return resp    #添加员工①


    @classmethod
    def query_emp(cls,headers,id):
        url=BASE_URL+"/api/sys/user"+"/"+id
        resp1=requests.get(url,headers=headers)
        return resp1   #查询员工2

    @classmethod
    def modify_emp(self,headers,id,body):
        url = BASE_URL+"/api/sys/user" + "/" + id
        resp3=requests.put(url,headers=headers,json=body)
        return resp3    #修改员工3

    @classmethod
    def delete_emp(cls,headers,id):
        url = BASE_URL+"/api/sys/user" + "/" + id
        resp4=requests.delete(url,headers=headers)
        return resp4    #删除员工4

if __name__ == '__main__':
    # headers = {"Content-Type": "application/json",
    #            "Authorization":"Bearer 00940306-4bee-4812-b4d7-57c33fd246ff" }
    # body_add = {"username": "YG001", "mobile": "18210222262", "workNumber": "123"}
    # resp=EmployeeApi.add_emp(headers,body_add)
    # print(resp.json())     #添加员工①

    headers=get_headers()
    body_add = {"username": "YG002", "mobile": "18235227260", "workNumber": "123"}
    resp = EmployeeApi.add_emp(headers, body_add)
    id=resp.json().get("data").get("id")  #添加员工获取id

    # 查询员工2
    resp2=EmployeeApi.query_emp(headers, id)
    print(resp2.json())


    #修改员工id
    headers=get_headers()
    body_modify = ({"username": "YG002", "password": "123456", "departmentId": "1211"})
    resp_modify=EmployeeApi.modify_emp(headers,id,body_modify)
    print("修改员工:",resp_modify.json())


    #删除员工

    resp_delete=EmployeeApi.delete_emp(headers,id)
    print("删除员工:",resp_delete.json())
